package blog.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.*;

public class LogUtils {
    final static String PLAT_NOKIA = "nokia";
    final static String PLAT_SE = "sonyericsson";
    final static String PLAT_EMULATE = "j2me";
    
    static String filePath;
    static FileConnection fileConn;
    static OutputStream os;
	
	public static String getDefaultResPath()
	{
		String sPlat = _getPlatform(), sPath;
	    if( sPlat.indexOf(PLAT_SE) >= 0 )
	        sPath = System.getProperty("fileconn.dir.graphics");
	    else
	        sPath = System.getProperty("fileconn.dir.photos");
	    
	    if( sPath == null && sPlat.indexOf(PLAT_SE) >= 0 )
	        sPath = "file:///Nand/Picture/";
	    
	    return sPath;
	}
	
	public static String _getPlatform()
    {
        String sPlat = System.getProperty("microedition.platform");
        
        if( sPlat == null )
            sPlat = PLAT_EMULATE;
        else
            sPlat = sPlat.toLowerCase();
        
        return sPlat;
    }
	
	public static String getCardRoot()
	{
		String sRootPath;
		String sPlat = _getPlatform();
		if( sPlat.toLowerCase().indexOf(PLAT_NOKIA) >= 0 )
            sRootPath = System.getProperty("fileconn.dir.memorycard");
        else if( sPlat.toLowerCase().indexOf(PLAT_SE) >= 0 )
        {
            if( _isRootExist("file:///Ms/") )
                return "file:///Ms/Other/";
            else if( _isRootExist("file:///e:/") )
                return "file:///e:/Other/";
            else
                return null;
        }
        else if( sPlat.toLowerCase().indexOf(PLAT_EMULATE) >= 0 )
            sRootPath = "file:///root1/";
        else
            sRootPath = "file:///e:/";

        if( _isRootExist(sRootPath) )
            return sRootPath;
        else
            return null;
	}

	public static boolean _isRootExist(String sRootPath)
    {
        if( sRootPath == null )
            return false;
        
        String sRoot;
        Enumeration enumDrive = FileSystemRegistry.listRoots();
        while( enumDrive.hasMoreElements() )
        {
            sRoot = (String) enumDrive.nextElement();
            
            if( sRootPath.endsWith(sRoot) )
                return true;
        }
        return false;
    }

	public static String appendPath( String path, String fileName )
	{
		if ( path==null )
			return fileName;
		if ( path.endsWith("/") )
		{
			return path + fileName;
		}
		else
		{
			return path + "/" + fileName;
		}
	}
}